package com.xing.crm.web;

import com.xing.crm.pojo.*;
import com.xing.crm.service.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/** 这里存放前端展示的数据接口（不包括页面，页面在WebsiteController）
 * @author Mr.L
 * @version V1.0
 * @date 2021年08月09日 16:10
 */
@RequestMapping("/web")
@RestController
public class WebDataInterfaceController {

	@Resource
	private ProCategoryService proCategoryService; //类别
	@Resource
	private ProShopService proShopService; //店铺
	@Resource
	private ProApplyService proApplyService; //申请
	@Resource
	private ProProductService productService; //商品
	@Resource
	private ProSkuService proSkuService; //sku
	///////////////////////////////////////////////////////////展示端///////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////展示端///////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////展示端///////////////////////////////////////////////////////////

	/**
	 * 分类列表数据
	 * @param proCategoryPoJo
	 * @param session
	 * @param page
	 * @param limit
	 * @return
	 */
	@RequestMapping(value = "/category/list", method = RequestMethod.GET)
	public PageResult<ProCategoryPoJo> list(
			ProCategoryPoJo proCategoryPoJo, HttpSession session,
			@RequestParam(required = false, defaultValue = "1") Integer page,
			@RequestParam(required = false, defaultValue = "10") Integer limit
	) {
		Boolean isManager = (Boolean) session.getAttribute("isManager");
		Boolean isClerk = (Boolean) session.getAttribute("isClerk");
		PageResult<ProCategoryPoJo> result = new PageResult();
		result.setCode(0);
		result.setMsg("没有权限");
		result.setData(new ArrayList());
		if(isManager != null || isClerk != null) {
			result = proCategoryService.listForWeb(proCategoryPoJo, page, limit);
		}
		return result;
	}
	
	//获取分类数据
	@RequestMapping(value = "/category/get", method = RequestMethod.GET)
	public Map<String, Object> categoryGet(
			ProCategoryPoJo proCategoryPoJo, HttpSession session,
			@RequestParam(required = false, defaultValue = "0") Integer id
	) {
		Map<String, Object> result = new HashMap<>();
		result.put("code",0);
		result.put("msg","操作成功");
		result.put("data",proCategoryService.get(id));
		return result;
	}

	/**
	 * 功能描述: 商品列表
	 * */
	@RequestMapping(value = "/productList",method = RequestMethod.GET)
	public PageResult<ProProductPoJo> productList(
		HttpSession session,
		@RequestParam(required = false, defaultValue = "1") Integer page,
		@RequestParam(required = false, defaultValue = "10") Integer limit,
		ProProductPoJo product
	) {
		/* 店长 */
		Boolean isManager = (Boolean) session.getAttribute("isManager");
		/* 店员 */
		Boolean isClerk = (Boolean) session.getAttribute("isClerk");
		WebUser user = (WebUser) session.getAttribute("webUser");
		//读取数据
		return productService.productList(user, product, isManager, isClerk, page, limit);
	}


	/**
	 * 功能描述: 产品详情
	 * */
	@RequestMapping(value = "/product/get", method = RequestMethod.GET)
	public Map<String, Object> getProduct(HttpSession session, Integer id) {
		Map<String, Object> result = new HashMap(0);
		result.put("code", 0);
		result.put("msg", "没有权限");
		Boolean isManager = (Boolean) session.getAttribute("isManager");
		Boolean isClerk = (Boolean) session.getAttribute("isClerk");
		/* 是否店员、店长 */
		boolean permit = (isManager != null && isManager) || (isClerk != null && isClerk);
		ProProductPoJo product = new ProProductPoJo();
		//只有店员或店长才能显示数据
		if(permit) {
			if(id != null) {
				product = this.productService.detail(id);
				product.setFilesPath(this.productService.allFiles(id));
				result.put("msg", "查询成功");
			}
		}
		//sku
		List sku = new ArrayList();
		ProSkuPoJo proSkuPoJo = new ProSkuPoJo();
		proSkuPoJo.setPid(product.getId());
		PageResult<ProSkuPoJo> skuList = proSkuService.list(proSkuPoJo,1,100);
		sku.addAll(skuList.getData());
		
		//输出
		result.put("sku", sku);
		result.put("data", product);
		return result;
	}

	///////////////////////////////////////////////////////////uc///////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////uc///////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////uc///////////////////////////////////////////////////////////

	/**
	 * 功能描述: 提交申请
	 * @date 2021/8/11 11:42
	 * @param proApply
	 * @return java.util.Map<java.lang.String,java.lang.Object>
	 * */
	@RequestMapping(value = "/shop/apply", method = RequestMethod.POST)
	public Map<String, Object> submitApply(ProApply proApply, HttpSession session) {
		WebUser webUser = (WebUser) session.getAttribute("webUser");
		return proApplyService.submitApply(proApply, webUser);
	}
	
	/**
	 * 功能描述: 店铺列表
	 * @date 2021/8/11 11:42
	 * @param
	 * */
	@RequestMapping(value = "/shop/list", method = RequestMethod.GET)
	public PageResult<ProShop> list(){
		return proShopService.getShops();
	}
	
	/**
	 * 功能描述: 店铺待审核列表
	 * @date 2021/8/11 11:43
	 * @param session
	 * */
	@RequestMapping(value = "/shop/applyList", method = RequestMethod.GET)
	public PageResult<ProApply> getApplies(HttpSession session) {
		/* 是否店长 */
		Boolean isManager = (Boolean) session.getAttribute("isManager");
		WebUser webUser = (WebUser) session.getAttribute("webUser");
		/* 所属店铺 */
		ProShop shop = proShopService.getShopByUid(webUser);
		return proApplyService.getApplies(shop, isManager);
	}
	
	/**
	 * 功能描述: 店员列表
	 * @date 2021/8/11 15:41
	 * @param session
	 * @return com.xing.crm.model.PageResult<java.util.Map<java.lang.String,java.lang.Object>>
	 * */
	@RequestMapping(value = "/shop/clerkList", method = RequestMethod.GET)
	public PageResult<Map<String, Object>> getClerks(HttpSession session) {
		/* 是否店长 */
		Boolean isManager = (Boolean) session.getAttribute("isManager");
		WebUser webUser = (WebUser) session.getAttribute("webUser");
		/* 所属店铺 */
		ProShop shop = proShopService.getShopByUid(webUser);
		return proShopService.getShopClerks(shop, isManager);
	}
	
	/**
	 * 功能描述: 同意申请
	 * @date 2021/8/11 15:45
	 * @param id
	 * @param session
	 * @return com.xing.crm.model.PageResult
	 * */
	@RequestMapping(value = "/shop/agreeApply", method = RequestMethod.POST)
	public PageResult agreeApply(String id, HttpSession session) {
		Boolean isManager = (Boolean) session.getAttribute("isManager");
		return proApplyService.agreeApply(id, isManager);
	}
	
	/**
	 * 功能描述: 移出店铺
	 * @date 2021/8/12 9:37
	 * @param session
	 * @param uid
	 * @param sid
	 * @return com.xing.crm.model.PageResult
	 * */
	@RequestMapping(value = "/shop/kickOut", method = RequestMethod.POST)
	public PageResult kickOut(HttpSession session, Integer uid, Integer sid) {
		WebUser user = (WebUser) session.getAttribute("webUser");
		Boolean isManager = (Boolean) session.getAttribute("isManager");
		return proShopService.delShopUser(uid, sid, user, isManager);
	}
	
	/**
	 * 功能描述: 产品列表
	 * * vbyzc:注意，他妈的，展示前台和用户中心的商品管理都是这个接口，普通店员也是用这个接口来显示产品列表
	 * 用户中心的产品管理是决定说不翻页的，所以产品列表翻页就有bug 20210906
	 * @date 2021/8/12 9:38
	 * @param category
	 * @param session
	 * @return com.xing.crm.model.PageResult<com.xing.crm.model.ProProductPoJo>
	 * */
	@RequestMapping(value = "/shop/products", method = RequestMethod.GET)
	public PageResult<ProProductPoJo> getShopProducts(Integer category, HttpSession session,
													  @RequestParam(required = false, defaultValue = "1") Integer page,
													  @RequestParam(required = false, defaultValue = "999") Integer limit) {
		WebUser user = (WebUser) session.getAttribute("webUser");
		Boolean isManager = (Boolean) session.getAttribute("isManager");
		Boolean isClerk = (Boolean) session.getAttribute("isClerk");
		return productService.getShopProducts(user, isManager, isClerk, category, page, limit);
	}
	
	/**
	 * 功能描述: 添加店铺在售产品
	 * @date 2021/8/12 11:00
	 * @param session
	 * @param pid
	 * @return com.xing.crm.model.PageResult
	 * */
	@RequestMapping(value = "/shop/onSaleProduct", method = RequestMethod.POST)
	public PageResult onSaleProduct(HttpSession session, Integer pid) {
		WebUser user = (WebUser) session.getAttribute("webUser");
		Boolean isManager = (Boolean) session.getAttribute("isManager");
		return productService.shopOnSaleProduct(user, pid, isManager);
	}
	
	/**
	 * 功能描述: 取消在售产品
	 * @date 2021/8/12 11:33
	 * @param session
	 * @param pid
	 * @return com.xing.crm.model.PageResult
	 * */
	@RequestMapping(value = "/shop/cancelSaleProduct", method = RequestMethod.POST)
	public PageResult cancelSaleProduct(HttpSession session, Integer pid) {
		WebUser user = (WebUser) session.getAttribute("webUser");
		Boolean isManager = (Boolean) session.getAttribute("isManager");
		return productService.cancelSaleProduct(user, pid, isManager);
	}


}
